import Event from './event.js'

class Rect extends Event {
  constructor (opts, canvas) {
    super()
    this.canvas = canvas
    this.config = opts
  }

  // 绘制rect方法
  draw () {
    const ctx = this.canvas.ctx
    const { x, y, width, height, fillStyle } = this.config
    ctx.fillStyle = fillStyle
    ctx.fillRect(x, y, width, height)
  }

  // canvas坐标系中的x和y
  isHit (cx, cy) {
    const { x, y, width, height } = this.config
    // console.log('childX:', x, 'childY:', y, 'cx', cx, 'cy', cy)
    // console.log('x + width:', x + width, 'y + height:', y + height)
    // console.log('x < cx:', x < cx, 'cx < x + width:', cx < x + width, 'cy < y + height:', cy < y + height, 'y <= cy:', y <= cy)
    // console.log('isHit', x < cx && cx < x + width && y <= cy && cy < y + height)
    // console.log('----------------------------------------------------------------')

    if (
      x < cx &&
      cx < x + width &&
      y <= cy &&
      cy < y + height
    ) {
      return true
    }
    return false
  }
}

export default Rect
